<?php

namespace App\Http\Controllers\Admin;

use App\Exceptions\CreateException;
use App\Exceptions\DeleteException;
use App\Exceptions\UpdateException;
use App\Item;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Craftsman;
use App\Needwork;
use App\Xm_register;

/**
 * @property Item model
 */
class ItemController extends Controller
{
    public function __construct(Item $model)
    {
        $this->model = $model;
    }

    /**
     * Display a listing of the resource.
     *
     * @param $crafts
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function index($crafts,Request $request)
    {
        $page  = $request->get('page',1);
        $size  = $request->get('size',10);
        $list = $this->model->where('craftmans_id',$crafts)->limit($size)->offset(($page-1)*$size)->get();
        $count = $this->model->where('craftmans_id',$crafts)->count();
        return $this->page($list,$count);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws CreateException
     */
    public function store(Request $request)
    {
        $data = $request->only(['craftmans_id','name','city','time','overed_at']);
        if((new \App\Logic\Craftsman())->addItem($data)){
            return $this->success();
        }
        throw new CreateException();
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
       // $data = $this->model->where('id','=',$id);
       // var_dump($data);
       $n = Craftsman::find($id);
       if($n-> verify == '4') {
            return $this->failed(202);
       }else{
            $n -> verify = '4';
            $n -> save();
            return $this->success();
       }
       throw new UpdateException();
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\JsonResponse
     * @throws DeleteException
     * @throws \Exception
     */
    public function destroy($id)
    {
        if ($this->model->whereId($id)->delete())
            return $this->success();
        throw new DeleteException();
    }
    public function shenhe($id)
    {
        $data = Xm_register::find($id);
        if($data['shenhe'] == '4') {
            return $this->failed(202,'已审核过的请勿再点击');
        }else{
            $data -> shenhe = '4';
            $data -> save();
            return $this->success();
        }
    }
    public function dit($id)
    {
        $data =  Needwork::find($id);
        if($data['shenhe'] == '4') {
            return $this->failed(202,'已审核过的请勿再点击');
        }else{
            $data['shenhe'] = '4';
            $data -> save();
            return $this->success();
        }
    }
}
